// pages/healthlife/healthlife.js
Page({
  data: {
    item: 0, // 标签页当前显示页面的序号
    categories: [
      { id: 0, name: '全部资讯' },
      { id: 1, name: '健康科普' },
      { id: 2, name: '养生保健' },
      { id: 3, name: '专家访谈' },
      { id: 4, name: '活动动态' }
    ],
    activeCategory: 0,
    allNews: [
      // 假数据，实际可从后端获取
      {
        id: 1,
        img: '/images/ceshi1.jpg',
        title: '中国健康研究院成立',
        desc: '中国健康研究院正式成立，致力于推动健康产业发展。',
        date: '2024-05-01',
        category: 0
      },
      {
        id: 2,
        img: '/images/ceshi2.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 1
      },
      {
        id: 3,
        img: '/images/ceshi3.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 2
      },
      {
        id: 4,
        img: '/images/ceshi4.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 1
      },
      {
        id: 5,
        img: '/images/ceshi2.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 3
      },
      {
        id: 6,
        img: '/images/ceshi3.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 4
      },
      {
        id: 7,
        img: '/images/ceshi3.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 1
      },
      {
        id: 8,
        img: '/images/ceshi4.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 0
      },
      {
        id: 9,
        img: '/images/ceshi1.jpg',
        title: '专家解读健康生活方式',
        desc: '专家为您解读科学健康的生活方式。',
        date: '2024-05-02',
        category: 1
      },
      // ...更多资讯
    ],
    newsList: [],
    page: 1,
    pageSize: 6,
    totalPage: 1,
    searchKeyword: '',
    activeTab: 0,
    recommendData: [
      // 食疗养生
      [
        { id: 1, img: '/images/gouqi.jpg', title: '红枣枸杞茶', desc: '补气养血，适合体虚人群。' },
        { id: 2, img: '/images/ceshi4.jpg', title: '黑芝麻糊', desc: '润肠通便，乌发养颜。' },
        { id: 3, img: '/images/lianzi.jpg', title: '银耳莲子羹', desc: '滋阴润肺，美容养颜。' },
        { id: 4, img: '/images/ceshi3.jpg', title: '山药排骨汤', desc: '健脾养胃，增强体质。' },
        { id: 5, img: '/images/jiang-tea.jpg', title: '苹果雪梨汤', desc: '润肺止咳，清热生津。' },
        { id: 6, img: '/images/ceshi2.jpg', title: '桂圆红豆粥', desc: '补血安神，健脾益气。' },
        { id: 7, img: '/images/ceshi1.jpg', title: '花生红豆粥', desc: '健脾益气' },
        { id: 8, img: '/images/counselor.jpg', title: '小米红豆粥', desc: '补血安神' },
      ],
      // 自我保健
      [
        { id: 1, img: '/images/ceshi1.jpg', title: '早晚梳头', desc: '促进头部血液循环，预防脱发。' },
        { id: 2, img: '/images/ceshi2.jpg', title: '泡脚养生', desc: '促进血液循环，助眠安神。' },
        { id: 3, img: '/images/ceshi3.jpg', title: '八段锦锻炼', desc: '强身健体，舒展筋骨。' },
        { id: 4, img: '/images/ceshi4.jpg', title: '眼保健操', desc: '缓解眼疲劳，保护视力。' },
        { id: 5, img: '/images/ceshi3.jpg', title: '午休小憩', desc: '缓解疲劳，提高下午精力。' },
        { id: 6, img: '/images/ceshi1.jpg', title: '腹式呼吸', desc: '增强肺活量，放松身心。' },
        { id: 7, img: '/images/ceshi4.jpg', title: '穴位按摩', desc: '疏通经络，调节气血。' },
        { id: 8, img: '/images/ceshi3.jpg', title: '艾灸养生', desc: '温经通络，驱寒除湿。' },
        { id: 9, img: '/images/ceshi3.jpg', title: '推拿保健', desc: '舒筋活血，缓解疲劳。' },
        { id: 10, img: '/images/ceshi2.jpg', title: '太极拳', desc: '修身养性，强身健体。' },
      ],
      // 节气养生
      [
        { id: 1, img: '/images/ceshi2.jpg', title: '春季养肝', desc: '多吃绿色蔬菜，舒畅情志。' },
        { id: 2, img: '/images/ceshi3.jpg', title: '夏季养心', desc: '多饮水，适当午休，心情愉快。' },
        { id: 3, img: '/images/ceshi3.jpg', title: '秋季润肺', desc: '多吃梨、百合，防燥润肺。' },
        { id: 4, img: '/images/ceshi3.jpg', title: '冬季养肾', desc: '多食黑色食物，注意保暖。' },
        { id: 5, img: '/images/ceshi4.jpg', title: '防春困', desc: '适当锻炼，保证睡眠。' },
        { id: 6, img: '/images/ceshi3.jpg', title: '防秋燥', desc: '多饮水，保持空气湿润。' },
        { id: 7, img: '/images/ceshi1.jpg', title: '小满养生', desc: '健脾祛湿，预防暑热。' },
        { id: 8, img: '/images/ceshi2.jpg', title: '大暑养生', desc: '清热解暑，养心安神。' },
        { id: 9, img: '/images/ceshi3.jpg', title: '立秋养生', desc: '滋阴润燥，预防秋燥。' },
        { id: 10, img: '/images/ceshi3.jpg', title: '冬至养生', desc: '温补阳气，防寒保暖。' },
      ]
    ],
    recommendList: [],
    bmiHeight: '',
    bmiWeight: '',
    bmiAge: '',
    bmiResult: '',
    basicCourses: [
      {
        id: 1,
        img: '/images/ceshi3.jpg',
        title: '基础热身训练',
        teacher: '教练：李明',
        desc: '适合初学者的全身热身课程'
      },
      {
        id: 2,
        img: '/images/ceshi1.jpg',
        title: '拉伸放松',
        teacher: '教练：王芳',
        desc: '运动前后的全身拉伸'
      },
      {
        id: 3,
        img: '/images/ceshi2.jpg',
        title: '核心力量训练',
        teacher: '教练：张伟',
        desc: '提升核心稳定性'
      }
    ],
    advancedCourses: [
      {
        id: 1,
        img: '/images/ceshi2.jpg',
        title: 'KILLER HIIT CARDIO',
        teacher: '教练：Alex',
        desc: '高强度间歇燃脂训练'
      },
      {
        id: 2,
        img: '/images/ceshi1.jpg',
        title: '耐力锻炼',
        teacher: '教练：陈晨',
        desc: '提升有氧耐力'
      },
      {
        id: 3,
        img: '/images/ceshi3.jpg',
        title: '普拉提塑形',
        teacher: '教练：刘婷',
        desc: '改善体态，增强柔韧'
      }
    ]
  },
  onLoad() {
    // 默认显示第一个tab的推荐
    this.updateRecommendList();
    // 新增：初始化资讯列表
    this.filterNews();
  },
  onTabChange(e) {
    const idx = Number(e.currentTarget.dataset.idx);
    this.setData({
      activeTab: idx,
      page: 1
    }, this.updateRecommendList);
  },
  updateRecommendList() {
    const { activeTab, recommendData, page, pageSize } = this.data;
    const allList = recommendData[activeTab] || [];
    const totalPage = Math.ceil(allList.length / pageSize) || 1;
    const start = (page - 1) * pageSize;
    const end = start + pageSize;
    this.setData({
      recommendList: allList.slice(start, end),
      totalPage
    });
  },
  onPrevPage() {
    if (this.data.page > 1) {
      this.setData({
        page: this.data.page - 1
      }, this.updateRecommendList);
    }
  },
  onNextPage() {
    if (this.data.page < this.data.totalPage) {
      this.setData({
        page: this.data.page + 1
      }, this.updateRecommendList);
    }
  },
  onRecommendPrevPage() {
    if (this.data.page > 1) {
      this.setData({
        page: this.data.page - 1
      }, this.updateRecommendList);
    }
  },
  onRecommendNextPage() {
    if (this.data.page < this.data.totalPage) {
      this.setData({
        page: this.data.page + 1
      }, this.updateRecommendList);
    }
  },
  // 顶部标签的点击函数
  changeItem(e) {
    const idx = Number(e.currentTarget.dataset.item);
    this.setData({
      item: idx
    }, () => {
      if (idx === 0) {
        this.filterNews();
      }
    });
  },
  // 滑块Swiper组件的滑动监听函数
  changeTab(e) {
    const idx = e.detail.current;
    this.setData({
      item: idx
    }, () => {
      if (idx === 0) {
        this.filterNews();
      }
    });
  },
  onSwiperChange(e) {
    this.setData({
      item: e.detail.current
    });
  },
   // 分类切换
   onCategoryTap(e) {
    const idx = e.currentTarget.dataset.idx;
    this.setData({
      activeCategory: idx,
      page: 1,
      searchKeyword: ''
    }, this.filterNews);
  },

  // 搜索
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },
  onSearch() {
    this.setData({
      page: 1
    }, this.filterNews);
  },

  // 分页
  onPrevPage() {
    if (this.data.page > 1) {
      this.setData({
        page: this.data.page - 1
      }, this.filterNews);
    }
  },
  onNextPage() {
    if (this.data.page < this.data.totalPage) {
      this.setData({
        page: this.data.page + 1
      }, this.filterNews);
    }
  },

  // 过滤资讯
  filterNews() {
    let filtered = this.data.allNews;
    // 分类过滤
    if (this.data.activeCategory != 0) {
      filtered = filtered.filter(item => item.category == this.data.activeCategory);
    }
    // 搜索过滤
    if (this.data.searchKeyword) {
      filtered = filtered.filter(item =>
        item.title.includes(this.data.searchKeyword) ||
        item.desc.includes(this.data.searchKeyword)
      );
    }
    // 分页
    const totalPage = Math.ceil(filtered.length / this.data.pageSize) || 1;
    const start = (this.data.page - 1) * this.data.pageSize;
    const end = start + this.data.pageSize;
    this.setData({
      newsList: filtered.slice(start, end),
      totalPage
    });
  },

  onBmiHeightInput(e) {
    this.setData({ bmiHeight: e.detail.value });
  },
  onBmiWeightInput(e) {
    this.setData({ bmiWeight: e.detail.value });
  },
  onBmiAgeInput(e) {
    this.setData({ bmiAge: e.detail.value });
  },
  onBmiCalc() {
    const height = parseFloat(this.data.bmiHeight) / 100;
    const weight = parseFloat(this.data.bmiWeight);
    if (!height || !weight) {
      this.setData({ bmiResult: '请输入正确的身高和体重' });
      return;
    }
    const bmi = (weight / (height * height)).toFixed(1);
    let level = '';
    if (bmi < 18.5) level = '偏瘦';
    else if (bmi < 24) level = '正常';
    else if (bmi < 28) level = '超重';
    else level = '肥胖';
    this.setData({ bmiResult: `BMI：${bmi}（${level}）` });
  }
})
